package org.yxing.palpalgps.adapter;

import java.util.ArrayList;
import java.util.List;

import org.yxing.palpalgps.R;
import org.yxing.palpalgps.domain.AsyncImageLoader;
import org.yxing.palpalgps.models.ListUserItem;
import org.yxing.palpalgps.utils.Helper;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * friend alerts
 * @author yuxing
 *
 */
public class FriendAlertAdapter extends BaseAdapter {
    
    private Context mContext;
    private List<ListUserItem> data;
    AsyncImageLoader asyncImageLoader;
    
    public FriendAlertAdapter(Context context) {
        mContext = context;
        data = new ArrayList<ListUserItem>();
        asyncImageLoader = new AsyncImageLoader();
    }

    public int getCount() {
        return data.size();
    }

    public ListUserItem getItem(int position) {
        return data.get(position);
    }

    public long getItemId(int position) {
        return getItem(position).getUid();
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.user_list_item, null);
        
        ImageView faceView = (ImageView) convertView.findViewById(R.id.faceView);
        TextView nameView = (TextView) convertView.findViewById(R.id.nameView);
        TextView genderView = (TextView) convertView.findViewById(R.id.genderView);
        ListUserItem model = getItem(position);
        
        nameView.setText(model.getName());
        genderView.setText(model.getGender().equals("m") ? 
                    mContext.getString(R.string.male) : 
                    mContext.getString(R.string.female));
        if (!TextUtils.isEmpty(model.getPortrait()))
            asyncImageLoader.loadDrawable(Helper.getSmallPictureUrl(model.getPortrait()), faceView);
        
        return convertView;
    }
    
    public void add(ListUserItem listUserItem) {
        data.add(listUserItem);
        notifyDataSetChanged();
    }
    
    public void remove(int position) {
        data.remove(position);
        notifyDataSetChanged();
    }

    public void clear() {
        data.clear();
    }
}
